package com.jvxb.common.utils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author jvxb
 * @since 2022-01-30
 */
public class IPUtil {


    public static boolean isPortUsed(String ip, int port) {

        int count = 0;
        int limit = 3;
        while (count < limit) {
            Socket sock = null;
            try {
                sock = new Socket();
                sock.connect(new InetSocketAddress(ip, port), 2000);
                return true;
            } catch (Exception e) {
                if (e.toString().contains("Connection refused")) {
                    return false;
                }
            } finally {
                if (sock != null) {
                    try {
                        sock.close();
                    } catch (IOException e) {
                    }
                }
            }
            count++;
        }

        return false;
    }
}
